﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using RestSemitaService.Models;

namespace RestSemitaService.Controllers
{
    public class CustomerController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/messenger/5
        public string Get(string username)
        {
            try
            {
                SqlConnection thisConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
                thisConnection.Open();
                SqlCommand thisCommand = thisConnection.CreateCommand();
				thisCommand.CommandText = @" select cust.customer_id as id, tbuser.username username, 
													tbuser.firstname firstname, tbuser.lastname lastname,
													'' as user_desc
											from	tb_user tbuser inner join customer cust on tbuser.ref_id = cust.customer_id
											where	username = @username";
                thisCommand.Parameters.AddWithValue("@username", username);
                using (SqlDataReader reader = thisCommand.ExecuteReader())
                {
                    List<Messenger> invoiceHeads = new List<Messenger>();
                    while (reader.Read())
                    {
                        string _id = reader["id"].ToString();
                        string _username = reader["username"].ToString();
                        string _user_desc = reader["user_desc"].ToString();
                        string _firstname = reader["firstname"].ToString();
                        string _lastname = reader["lastname"].ToString();
                        invoiceHeads.Add(new Messenger
                        {
                            Id = _id,
                            Username = _username,
                            UserDesc = _user_desc,
                            FirstName = _firstname,
                            LastName = _lastname
                        });
                    }
                    reader.Close();
                    thisConnection.Close();
                    string json = JsonConvert.SerializeObject(new
                    {
                        InvoiceHead = invoiceHeads
                    });
                    return json;
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.Message);
                return e.StackTrace + " \n >>>>>>>>>>>>>>>>> \n" + e.Message;
            }
        }

        // POST api/messenger
        public void Post([FromBody]string value)
        {
        }

        // PUT api/messenger/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/messenger/5
        public void Delete(int id)
        {
        }
    }
}
